package com.itheima.utils;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.format.DateTimeFormatter;

/**
 * @ClassName: Java8DateTimeUtil
 * @Description:
 * @author: Yrd
 * @date: 2021-10-22 9:34
 */
public class Java8DateTimeUtil {
    /**
     *
     * @Title : getStartOrEndDayOfWeek
     * @Description : 获取本周的第一天或最后一天

     * @param today
     * @param isFirst true 表示开始时间，false 表示结束时间
     * @return java.lang.String
     *
     * @user : Yrd
     * @date : 2021-10-22 10:06
     */
    public static String getStartOrEndDayOfWeek(LocalDate today,boolean isFirst){
        LocalDate resDate = LocalDate.now();
        if(today == null){
            today = resDate;
        }
        DayOfWeek week = today.getDayOfWeek();
        int value = week.getValue();
        if(isFirst){
            resDate = today.minusDays(value-1);
        }else {
            resDate = today.plusDays(7-value);
        }
        return resDate.toString();

    }

    /**
     *
     * @Title : getStartOrEndDayOfMonth
     * @Description : 获取本月的第一天或最后一天

     * @param today
     * @param isFirst  true 表示开始时间，false表示结束时间
     * @return java.lang.String
     *
     * @user : Yrd
     * @date : 2021-10-22 10:24
     */
    public static String getStartOrEndDayOfMonth(LocalDate today,boolean isFirst){
        LocalDate resDate = LocalDate.now();
        if(today == null){
            today = resDate;
        }
        Month month = today.getMonth();
        int length = month.length(today.isLeapYear());
        if(isFirst){
            resDate = LocalDate.of(today.getYear(),month,1);
        }else {
            resDate = LocalDate.of(today.getYear(),month,length);
        }
        return resDate.toString();
    }

    /**
     *
     * @Title : getStartOrEndDayOfQuarter
     * @Description : 获取本季度的第一天或最后一天

     * @param today
     * @param isFirst
     * @return java.lang.String
     *
     * @user : Yrd
     * @date : 2021-10-22 10:38
     */
    public static String getStartOrEndDayOfQuarter(LocalDate today,boolean isFirst){
        LocalDate resDate = LocalDate.now();
        if(today == null){
            today = resDate;
        }
        Month month = today.getMonth();
        Month firstMonthOfQuarter = month.firstMonthOfQuarter();
        Month endMonthOfQuarter = Month.of(firstMonthOfQuarter.getValue() + 2);
        if(isFirst){
            resDate = LocalDate.of(today.getYear(),firstMonthOfQuarter,1);
        }else {
            resDate = LocalDate.of(today.getYear(),endMonthOfQuarter,endMonthOfQuarter.length(today.isLeapYear()));
        }
        return resDate.toString();
    }

    /**
     *
     * @Title : getStartOrEndOfYear
     * @Description : 获取本年的第一天或最后一天

     * @param today
     * @param isFirst
     * @return java.lang.String
     *
     * @user : Yrd
     * @date : 2021-10-22 10:43
     */
    public static String getStartOrEndOfYear(LocalDate today,boolean isFirst){
        LocalDate resDate = LocalDate.now();
        if(today == null){
            today = resDate;
        }
        if(isFirst){
            resDate = LocalDate.of(today.getYear(),Month.JANUARY,1);
        }else {
            resDate = LocalDate.of(today.getYear(),Month.DECEMBER,Month.DECEMBER.length(today.isLeapYear()));
        }
        return resDate.toString();
    }

    /**
     *
     * @Title : formatDate
     * @Description : 字符串转化成日期

     * @param strDate
     * @param pattern
     * @return java.time.LocalDate
     *
     * @user : Yrd
     * @date : 2021-10-22 19:08
     */
    public static LocalDate formatStrToDate(String strDate,String pattern){
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
        return LocalDate.parse(strDate,dateTimeFormatter);
    }

    /**
     *
     * @Title : formatDateToStr
     * @Description : 日期转化成字符串

     * @param date
     * @param pattern
     * @return java.lang.String
     *
     * @user : Yrd
     * @date : 2021-10-22 19:31
     */
    public static String formatDateToStr(LocalDate date,String pattern){
        //DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
        //return dateTimeFormatter.format(date);
        return date.format(DateTimeFormatter.ofPattern(pattern));
    }

    public static void main(String[] args){
        //getStartOrEndDayOfQuarter(LocalDate.now(),true);
        LocalDate localDate = formatStrToDate("20190919", "yyyyMMdd");
        System.out.println(localDate.toString());
        String endDayOfQuarter = getStartOrEndDayOfQuarter(localDate, false);
        System.out.println(endDayOfQuarter);

        System.out.println(formatDateToStr(LocalDate.now(),"yyyyMMdd"));
    }
}



































